/**    
 * @{#} SecurityManagerImpl.java Create on Mar 10, 2012 12:27:07 AM    
 *    
 * Copyright (c) 2012 by 悠忧虫.    
 *    
 * @author   
 * @version 1.0    
 */
package tk.sunlatus.project.security.impl;

import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.bouncycastle.util.encoders.Hex;

import tk.sunlatus.project.common.constants.SunlatusConstants;
import tk.sunlatus.project.security.SunlotusSecurityManager;
import tk.sunlatus.project.security.decode.Decoder;
import tk.sunlatus.project.security.encode.Encoder;
import tk.sunlatus.project.security.key.KeyManager;

/**
 * @{# SecurityManagerImpl.java Create on Mar 10, 2012 12:27:07 AM
 * 
 *     类功能说明:
 * 
 *     <p>
 *     Copyright: Copyright(c) 2012
 *     </p>
 * @Version 1.0
 * @Author sunlotus
 */
public class SecurityManagerImpl implements SunlotusSecurityManager {

    private Encoder encoder = null;

    private Decoder decoder = null;

    private MessageDigest digest = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public Key getKey() {
        return KeyManager.getKey(SunlatusConstants.EncodeConstant.KEY_IDEA,
            SunlatusConstants.EncodeConstant.KEY_IDEA_SIZE);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getKeyHex(Key key) {
        return new String(Hex.encode(key.getEncoded()), SunlatusConstants.EncodeConstant.CHARSET);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String encode(String src, Key key) {
        return new String(Hex.encode(encoder.encrypt(key, src)), SunlatusConstants.EncodeConstant.CHARSET);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String encode(String src, String keyHexStr) {
        Key key = KeyManager.bytesToKey(Hex.decode(keyHexStr.getBytes(SunlatusConstants.EncodeConstant.CHARSET)));
        return encode(src, key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String decode(String encodedStr, String keyHexStr) {
        Key key = KeyManager.bytesToKey(Hex.decode(keyHexStr.getBytes(SunlatusConstants.EncodeConstant.CHARSET)));
        return decode(encodedStr, key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String decode(String encodeStr, Key key) {
        return new String(Hex.encode(decoder.decrypt(key, encodeStr)), SunlatusConstants.EncodeConstant.CHARSET);
    }

    public Encoder getEncoder() {
        return encoder;
    }

    public void setEncoder(Encoder encoder) {
        this.encoder = encoder;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String digest(String src, String salt) {
        String result = null;
        getDigest().update(salt.getBytes(SunlatusConstants.EncodeConstant.CHARSET));
        byte[] resultByte = digest.digest(src.getBytes(SunlatusConstants.EncodeConstant.CHARSET));
        getDigest().reset();
        result = new String(Hex.encode(resultByte), SunlatusConstants.EncodeConstant.CHARSET);
        return result;
    }

    /**
     * 方法描述：获取信息摘要对象
     * 
     * @return 信息摘要对象
     */
    public MessageDigest getDigest() {
        if (digest == null) {
            try {
                digest = MessageDigest.getInstance(SunlatusConstants.EncodeConstant.DIGEST_ALGORITHM);
            } catch (NoSuchAlgorithmException e) {
                // TODO 记录日志
                e.printStackTrace();
            }
        }
        return digest;
    }

}
